* Table 5
* Author: Xiao Wang
* Version: 07/2020 

cd "C:\Users\Xiao\Box\TRI_relocation_project\Paper1\Replication_kit"
set more off

use data_performance, clear 


* ------------------------------------------- 
* Post-moving Performance
* -------------------------------------------

sort facilityID year
bysort facilityID m_indc_ctcnty: gen n1 = _n 
by facilityID: egen meanemp_afm_ctcnty = mean(emp) if m_indc_ctcnty > 0 &  2 <= n1 & n1 <= 4
by facilityID: egen meanlgemp_afm_ctcnty = mean(lgemp) if m_indc_ctcnty > 0 &  2 <= n1 & n1 <= 4

by facilityID: egen meanrelt500_afm_ctcnty = mean(totalrelt500) if m_indc_ctcnty > 0 &  2 <= n1 & n1 <= 4
by facilityID: egen meanrelt500emp_afm_ctcnty = mean(relt500emp) if m_indc_ctcnty > 0 &  2 <= n1 & n1 <= 4
by facilityID: egen meanlgrelt500_afm_ctcnty = mean(lgtotalrelt500) if m_indc_ctcnty > 0 &  2 <= n1 & n1 <= 4
by facilityID: egen meanlgrelt500emp_afm_ctcnty = mean(lgrelt500emp) if m_indc_ctcnty > 0 &  2 <= n1 & n1 <= 4

by facilityID: egen meanemp_afm = max(meanemp_afm_ctcnty)
by facilityID: egen meanlgemp_afm = max(meanlgemp_afm_ctcnty)

by facilityID: egen meanrelt500_afm = max(meanrelt500_afm_ctcnty) 
by facilityID: egen meanrelt500emp_afm = max(meanrelt500emp_afm_ctcnty) 
by facilityID: egen meanlgrelt500_afm = max(meanlgrelt500_afm_ctcnty) 
by facilityID: egen meanlgrelt500emp_afm = max(meanlgrelt500emp_afm_ctcnty) 

drop *_afm_ctcnty
*

* ----------------------------------------
* Pre-moving Performance 
* ----------------------------------------
keep if m_indc_ctcnty == 0 

// 1. Check the real case 
preserve 
sort facilityID year

//keep if firstTRI == 1990 | firstTRI == 1991

// Identify light polluters
capture drop noreport_bf
by facilityID: egen noreport_bf = max(censorall == _N) 
count if noreport_bf == 1 & moving_ctcnty == 1  // N = 1673(censorall). N = 1891(zeroall)
count if noreport_bf == 1 & moving_cnty == 1  // N = 802(censorall). N = 904(zeroall)


// Generate mean value of emissions t years before moving
gsort facilityID -year 
by facilityID: gen n = _n 
by facilityID: egen meanemp_bfm_ctcnty = mean(emp) if 3 <= n & n <= 5
by facilityID: egen meanlgemp_bfm_ctcnty = mean(lgemp) if 3 <= n & n <= 5

by facilityID: egen meanrelt500_bfm_ctcnty = mean(totalrelt500) if 3 <= n & n <= 5
by facilityID: egen meanrelt500emp_bfm_ctcnty = mean(relt500emp) if 3 <= n & n <= 5
by facilityID: egen meanlgrelt500_bfm_ctcnty = mean(lgtotalrelt500) if 3 <= n & n <= 5
by facilityID: egen meanlgrelt500emp_bfm_ctcnty = mean(lgrelt500emp) if 3 <= n & n <= 5

	by facilityID: egen meanemp_bfm = max(meanemp_bfm_ctcnty)
	by facilityID: egen meanlgemp_bfm = max(meanlgemp_bfm_ctcnty) 

	by facilityID: egen meanrelt500_bfm = max(meanrelt500_bfm_ctcnty)
	by facilityID: egen meanrelt500emp_bfm = max(meanrelt500emp_bfm_ctcnty)
	by facilityID: egen meanlgrelt500_bfm = max(meanlgrelt500_bfm_ctcnty) 
	by facilityID: egen meanlgrelt500emp_bfm = max(meanlgrelt500emp_bfm_ctcnty) 
	
	drop *_bfm_ctcnty

// Check the hard zeros and facilities with one-year data before moving
count if maxmove_ctcnty == 1 & n >= 3 & n <= 5 & totalrelt == 0 & censor_tl == 0
count if maxmove_ctcnty == 1 & n >= 3 & n <= 5 & totalrelt == 0 & censor_tl == 1
codebook facilityID if maxmove_ctcnty == 1 & n >= 3 & n <= 5 & totalrelt == 0 & censor_tl == 0
codebook facilityID if maxmove_ctcnty == 1 & n >= 3 & n <= 5 & totalrelt == 0 & censor_tl == 1
codebook facilityID if maxmove_ctcnty == 1 & inlist(n,1,2) & meanemp_bfm == . & inlist(year,1990, 1991)  // n = 279
	
// Generate the difference 
capture drop diffrp_*
capture drop rdiffrp_* 
gen diffrp_emp = meanemp_afm - meanemp_bfm
gen diffrp_lgemp = meanlgemp_afm - meanlgemp_bfm

gen diffrp_relt500 = meanrelt500_afm - meanrelt500_bfm 
gen diffrp_relt500emp = meanrelt500emp_afm - meanrelt500emp_bfm

gen diffrp_lgrelt500 = meanlgrelt500_afm - meanlgrelt500_bfm
gen diffrp_lgrelt500emp = meanlgrelt500emp_afm - meanlgrelt500emp_bfm 

// Check the potential bias
count if moving_ctcnty == 1
count if meanrelt500_afm == 0.5 & meanrelt500_bfm < 0.5 & moving_ctcnty == 1 // N = 3,138
count if meanrelt500_bfm == 0.5 & meanrelt500_afm < 0.5 & moving_ctcnty == 1 
 
 
 
 
*-----------------
* T-test
* -----------------
sort facilityID year 
gen report_bf = 1 - noreport_bf

// levels
foreach i of varlist diffrp_emp diffrp_relt500 diffrp_relt500emp {

	ttest `i' == 0 if moving_ctcnty == 1
	bysort report_bf: ttest `i' == 0 if moving_ctcnty == 1	
	ttest `i' == 0 if moving_cnty == 1
	bysort report_bf: ttest `i' == 0 if moving_cnty == 1
	ttest `i' == 0 if moving_ct == 1
	bysort report_bf: ttest `i' == 0 if moving_ct == 1
		
	ttest `i' == 0 if lowerpressurez1 == 1 & moving_ctcnty == 1
	ttest `i' == 0 if lowerpressurez2 == 1 & moving_ctcnty == 1
	ttest `i' == 0 if higherpressurez1 == 1 & moving_ctcnty == 1
	ttest `i' == 0 if higherpressurez2 == 1 & moving_ctcnty == 1

	}

// logs (cannot do log with relt = 0) 

foreach i of varlist diffrp_lgemp diffrp_lgrelt500 diffrp_lgrelt500emp {
	
	ttest `i' == 0 if moving_ctcnty == 1 & diffrp_lgrelt500emp ~= .
	bysort report_bf: ttest `i' == 0 if moving_ctcnty == 1 & diffrp_lgrelt500emp ~= .
	ttest `i' == 0 if moving_cnty == 1 & diffrp_lgrelt500emp ~= .
	bysort report_bf: ttest `i' == 0 if moving_cnty == 1 & diffrp_lgrelt500emp ~= .
	ttest `i' == 0 if moving_ct == 1 & diffrp_lgrelt500emp ~= .
	bysort report_bf: ttest `i' == 0 if moving_ct == 1 & diffrp_lgrelt500emp ~= .
	
	ttest `i' == 0 if lowerpressurez1 == 1 & moving_ctcnty == 1 & diffrp_lgrelt500emp ~= .
	ttest `i' == 0 if lowerpressurez2 == 1 & moving_ctcnty == 1 & diffrp_lgrelt500emp ~= .
	ttest `i' == 0 if higherpressurez1 == 1 & moving_ctcnty == 1 & diffrp_lgrelt500emp ~= .
	ttest `i' == 0 if higherpressurez2 == 1 & moving_ctcnty == 1 & diffrp_lgrelt500emp ~= .	
	
	}

restore 



* -------------------------------------
* Placobo Test 
* -------------------------------------
preserve 
sort facilityID year

// Generate placebo movings
sort facilityID year
capture drop maxmove_ctcnty 
by facilityID: egen maxmove_ctcnty = max(moving_ctcnty)
keep if maxmove_ctcnty == 0  
count if year == 1990   

capture drop norm 
set seed 39264896
by facilityID year: gen norm = runiform()
capture drop N
by facilityID: gen N = _N - 2
replace norm = int(N * norm) 

capture drop fakemoving_ctcnty
by facilityID: gen fakemoving_ctcnty = (year == 1991 + norm[1])

// Identify non-reporters
capture drop fakem_indc_ctcnty
by facilityID: gen fakem_indc_ctcnty = sum(fakemoving_ctcnty)
replace fakem_indc_ctcnty = 0 if fakem_indc_ctcnty == 1 & fakemoving_ctcnty == 1
capture drop fakecensorall fakeN
capture drop fakezeroall
by facilityID: egen fakecensorall = sum(censor_tl) if fakem_indc_ctcnty == 0
by facilityID: egen fakeN = count(facilityID) if fakem_indc_ctcnty == 0 

capture drop fakenoreport_bf
by facilityID: egen fakenoreport_bf = max(fakecensorall == fakeN & fakecensorall ~= .)
count if fakenoreport_bf == 1 & fakemoving_ctcnty == 1  // N 3,273
count if fakemoving_ctcnty == 1  // 20,199

// Calculate the change after moving
sort facilityID year 
bysort facilityID fakem_indc_ctcnty: gen n2 = _n
by facilityID: egen fmeanemp_afm_ctcnty = mean(emp) if fakem_indc_ctcnty > 0 &  2 <= n2 & n2 <= 4
by facilityID: egen fmeanlgemp_afm_ctcnty = mean(lgemp) if fakem_indc_ctcnty > 0 & 2 <= n2 & n2 <= 4 & lgtotalrelt500 ~= .
by facilityID: egen fmeanemp_afm = max(fmeanemp_afm_ctcnty)
by facilityID: egen fmeanlgemp_afm = max(fmeanlgemp_afm_ctcnty) 

by facilityID: egen fmeanrelt500_afm_ctcnty = mean(totalrelt500) if fakem_indc_ctcnty > 0 &  2 <= n2 & n2 <= 4
by facilityID: egen fmeanrelt500emp_afm_ctcnty = mean(relt500emp) if fakem_indc_ctcnty > 0 &  2 <= n2 & n2 <= 4

by facilityID: egen fmeanrelt500_afm = max(fmeanrelt500_afm_ctcnty)
by facilityID: egen fmeanrelt500emp_afm = max(fmeanrelt500emp_afm_ctcnty)

by facilityID: egen fmeanlgrelt500_afm_ctcnty = mean(lgtotalrelt500) if fakem_indc_ctcnty > 0 &  2 <= n2 & n2 <= 4
by facilityID: egen fmeanlgrelt500emp_afm_ctcnty = mean(lgrelt500emp) if fakem_indc_ctcnty > 0 &  2 <= n2 & n2 <= 4

by facilityID: egen fmeanlgrelt500_afm = max(fmeanlgrelt500_afm_ctcnty) 
by facilityID: egen fmeanlgrelt500emp_afm = max(fmeanlgrelt500emp_afm_ctcnty) 

drop *_afm_ctcnty  

keep if fakem_indc_ctcnty == 0
count if fakenoreport_bf == 1 & fakemoving_ctcnty == 1  //
count if fakemoving_ctcnty == 1

// Generate mean value of emissions 2 to 4 years before moving
gsort facilityID -year 
by facilityID: gen n = _n 
by facilityID: egen fmeanemp_bfm_ctcnty = mean(emp) if 3 <= n & n <= 5
by facilityID: egen fmeanlgemp_bfm_ctcnty = mean(lgemp) if 3 <= n & n <= 5 & lgtotalrelt500 ~= .

by facilityID: egen fmeanrelt500_bfm_ctcnty = mean(totalrelt500) if 3 <= n & n <= 5
by facilityID: egen fmeanrelt500emp_bfm_ctcnty = mean(relt500emp) if 3 <= n & n <= 5

by facilityID: egen fmeanlgrelt500_bfm_ctcnty = mean(lgtotalrelt500) if 3 <= n & n <= 5
by facilityID: egen fmeanlgrelt500emp_bfm_ctcnty = mean(lgrelt500emp) if 3 <= n & n <= 5

    by facilityID: egen fmeanemp_bfm = max(fmeanemp_bfm_ctcnty)
	by facilityID: egen fmeanlgemp_bfm = max(fmeanlgemp_bfm_ctcnty) 

	by facilityID: egen fmeanrelt500_bfm = max(fmeanrelt500_bfm_ctcnty)
	by facilityID: egen fmeanrelt500emp_bfm = max(fmeanrelt500emp_bfm_ctcnty)

	by facilityID: egen fmeanlgrelt500_bfm = max(fmeanlgrelt500_bfm_ctcnty) 
	by facilityID: egen fmeanlgrelt500emp_bfm = max(fmeanlgrelt500emp_bfm_ctcnty) 

	drop *_bfm_ctcnty

// Calculate the difference 
capture drop fdiffrp_*
gen fdiffrp_emp = fmeanemp_afm - fmeanemp_bfm
gen fdiffrp_lgemp = fmeanlgemp_afm - fmeanlgemp_bfm

gen fdiffrp_relt500 = fmeanrelt500_afm - fmeanrelt500_bfm
gen fdiffrp_relt500emp = fmeanrelt500emp_afm - fmeanrelt500emp_bfm

gen fdiffrp_lgrelt500 = fmeanlgrelt500_afm - fmeanlgrelt500_bfm
gen fdiffrp_lgrelt500emp = fmeanlgrelt500emp_afm - fmeanlgrelt500emp_bfm 

// Do the test on before and after move difference
sort facilityID year 
gen fakereport_bf = 1 - fakenoreport_bf

foreach i of varlist fdiffrp_emp fdiffrp_relt500 fdiffrp_relt500emp {

	ttest `i' == 0 if fakemoving_ctcnty == 1 
	ttest `i' == 0 if fakemoving_ctcnty == 1 & fakereport_bf == 0
	ttest `i' == 0 if fakemoving_ctcnty == 1 & fakereport_bf == 1 
	
	} 
*	
foreach i of varlist fdiffrp_lgemp fdiffrp_lgrelt500 fdiffrp_lgrelt500emp {

	ttest `i' == 0 if fakemoving_ctcnty == 1 & fdiffrp_lgrelt500 ~= .
	ttest `i' == 0 if fakemoving_ctcnty == 1 & fakereport_bf == 0 & fdiffrp_lgrelt500 ~= .
	ttest `i' == 0 if fakemoving_ctcnty == 1 & fakereport_bf == 1 & fdiffrp_lgrelt500 ~= .
	
	}

restore


